(function (vc) {
    let DEFAULT_PAGE = 1;
    let DEFAULT_ROW = 10;
    vc.extends({
        data: {
            communityRoomTreeInfo: {
                units: [],
                callName: ''
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('communityRoomTree', 'initCommunityRoomTree', function (_param) {
                $that.communityRoomTreeInfo.callName = _param.callName;
                $that._loadCommunityUnits();
            });
        },
        methods: {
            _loadCommunityUnits: function () {
                let param = {
                    params: {
                        hc:1.8
                    }
                };
                //发送get请求
                vc.http.apiGet('/community.queryCommunityUnitTree',
                    param,
                    function (json) {
                        let _json = JSON.parse(json);
                        $that.communityRoomTreeInfo.units = _json.data;
                        $that._initJsTreeCommunityRoomTree();
                    },
                    function () {
                        console.log('请求失败处理');
                    });
            },
            _initJsTreeCommunityRoomTree: function () {
                let _data = $that.communityRoomTreeInfo.units; 
   
                $.jstree.destroy()
                $("#jstree_communityRoomTreeDiv").jstree({
                    "checkbox": {
                        "keep_selected_style": false
                    },
                    'state': { //一些初始化状态
                        "opened": true,
                    },
                    'core': {
                        "check_callback": true,
                        'data': _data
                    }
                });
                $("#jstree_communityRoomTreeDiv").on("ready.jstree", function (e, data) {
                    //data.instance.open_all();//打开所有节点
                    let _callName = $that.communityRoomTreeInfo.callName;
                    if (_callName == 'oweFeeCallable') {
                        return;
                    }
                    if(!_data[0] || !_data[0].children[0] || !_data[0].children[0].children[0]){
                        return;
                    }
                    $('#jstree_communityRoomTreeDiv').jstree('select_node', _data[0].children[0].children[0].id /* , true */);
                });
                $('#jstree_communityRoomTreeDiv').on("changed.jstree", function (e, data) {
                    if (data.action == 'model' || data.action == 'ready') {
                        //默认合并
                        //$("#jstree_floorUnit").jstree("close_all");
                        return;
                    }
                    let _selected = data.selected[0];
                    if (_selected.startsWith('f_')) {
                        return;
                    }
                    //console.log(_selected, data.node.original.unitId)
                    if (_selected.startsWith('u_')) {
                        $that._communityRoomTreeLoadRoom(data.node.original.unitId, data);
                    }
                    if (_selected.startsWith('r_')) {
                        vc.emit($that.communityRoomTreeInfo.callName, 'selectRoom', {
                            roomName: data.node.original.roomName,
                            roomId: data.node.original.roomId
                        })
                    }
                });
                $('#jstree_communityRoomTreeDiv')
                    .on('click', '.jstree-anchor', function (e) {
                        $(this).jstree(true).toggle_node(e.target);
                    })
            },
            _communityRoomTreeLoadRoom: function (_unitId, data) {
                //获取选中的节点
                let node = data.instance.get_node(data.selected[0]);
                let childNodes = node.children;
                //$('#u_' + _unitId)
                if (childNodes && childNodes.length > 0) {
                    $('#jstree_communityRoomTreeDiv').jstree('open_node', '#u_' + _unitId);
                    return;
                }
                let param = {
                    params: {
                        page: 1,
                        row: 1000,
                        unitId: _unitId,
                    }
                }
                //发送get请求
                let _datas = [];
                vc.http.apiGet('/room.queryAdminRoomsTree',
                    param,
                    function (json, res) {
                        let listRoomData = JSON.parse(json);
                        if (listRoomData.total < 1) {
                            vc.toast('未找到房屋');
                            return;
                        }
                        listRoomData.rooms.forEach(_room => {
                            let _text = _room.roomNum;
                            if (_room.ownerName) {
                                _text += ('(' + _room.ownerName + ")")
                            }
                            let _data = {
                                id: 'r_' + _room.roomId,
                                roomId: _room.roomId,
                                roomName: _room.floorNum + "-" + _room.unitNum + "-" + _room.roomNum,
                                text: _text,
                                icon: "/img/room.png",
                            };
                            _datas.push(_data);
                            // $('#jstree_communityRoomTreeDiv').jstree('create_node', $('#u_' + _unitId), _data, "last", false, false);
                        })
                        $('#jstree_communityRoomTreeDiv').jstree('_append_json_data', $('#u_' + _unitId), _datas, function () {
                            // 这个回调函数要加  不然会报错，即使这个函数里面什么也不做
                        });
                        setTimeout(function () {
                            $('#jstree_communityRoomTreeDiv').jstree('open_node', '#u_' + _unitId);
                        }, 1000);
                        if (listRoomData.rooms && listRoomData.rooms.length > 0) {
                            vc.emit($that.communityRoomTreeInfo.callName, 'selectRoom', {
                                roomName: listRoomData.rooms[0].floorNum + "-" + listRoomData.rooms[0].unitNum + "-" + listRoomData.rooms[0].roomNum,
                                roomId: listRoomData.rooms[0].roomId
                            })
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);